package org.jeecgframework.boot.springaimcpservice.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.jeecgframework.boot.springaimcpservice.service.IDunGouInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class DunGouInfoServiceImpl implements IDunGouInfoService {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    
    @Autowired
    private ObjectMapper objectMapper;
    
    @Override
    public List<Map<String,Object>> getDunGouInfo() throws JsonProcessingException {

            // 首先尝试使用StringRedisTemplate直接获取字符串
            String jsonData = stringRedisTemplate.opsForValue().get("doc:cache:DocList");
            if (jsonData != null && !jsonData.isEmpty()) {
                // jsonData转换成 List<Map<String,Object>>
                return objectMapper.readValue(jsonData, new com.fasterxml.jackson.core.type.TypeReference<List<Map<String, Object>>>() {});
            }

        // 如果Redis中没有数据或解析失败，这里应该从数据库获取并缓存
        return List.of();
    }

}
